﻿<template>
    <div id="app">
        <Spin size="large" fix v-if="spinShow"></Spin>
        <router-view></router-view>
    </div>
   
</template>

<script>
    import { Toast } from 'mint-ui';
    export default {
        name: 'app',
        data() {
            return {
                spinShow: true
            }
        },
        
        methods: {
            load: function () {
                var _this = this;
                var code = this.GetRequest("code");
             

                this.$api.post('wx/Index/getaccess_token', {
                    "code": code
                }, response => {
                    if (response.status >= 200 && response.status < 300) {
                        console.log(response.data);
                        //this.$data.code = response.data;
                        var obj =response.data;
                       
                        _this.login(obj.openid);
                        //请求成功，response为成功信息参数
                    } else {
                        console.log(response.message);
                        //请求失败，response为失败信息
                    }
                });
            },
            login: function (openid) {
                var _this = this;
                openid = "oN2FAuPtITnjUkCBv_Z_YU_qetyw";
                this.$api.post('wx/Index/login', {
                    "openid": openid
                }, response => {
                    if (response.status >= 200 && response.status < 300) {
                        var data = response.data;
                        console.log(data);
                       
                        if (data.status == 200) {
                            _this.$data.spinShow = false;
                            if (data.message == "登录成功") {
                                _this.$router.push({ name: 'App', params: { id: '1' } });
                            } else if (data.message == "未绑定") {
                                console.log("跳转");
                              
                                _this.$router.push({ name: 'login', params: { openid: openid} });
                            } else {
                                 _this.$router.push({ name: 'App', params: { id: '1' } });
                            }


                        } else {
                            Toast(data.message);
                        }
                        //this.$data.code = response.data;
                       
                        //请求成功，response为成功信息参数
                    } else {
                        Toast("请求失败");
                        console.log(response.message);
                        //请求失败，response为失败信息
                    }
                });
            },
            GetRequest: function (value) {
                var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
                var object = {};
                if (url.indexOf("?") != -1)//url中存在问号，也就说有参数。  
                {
                    var str = url.substr(1);  //得到?后面的字符串
                    var strs = str.split("&");  //将得到的参数分隔成数组[id="123456",Name="bicycle"];
                    for (var i = 0; i < strs.length; i++) {
                        object[strs[i].split("=")[0]] = strs[i].split("=")[1]
                    }
                }

                return object[value];
            }

        }, created() {
            this.load();
        }

    }
</script>

<style scoped>
    @import url("//unpkg.com/iview/dist/styles/iview.css");
   
    #app {
        background-color:#fff
    }

   

</style>